import tkinter as tk
import tkinter.font as tkFont
import tkinter.messagebox as msgbox
import question_library

def run_question(question_num):
    """根据题目序号运行对应的题目函数，并处理异常展示错误信息"""
    try:
        if question_num == 1:
            question_library.day_of_year_question()
        elif question_num == 2:
            question_library.prime_numbers_question()
        elif question_num == 3:
            question_library.grade_question()
        elif question_num == 4:
            question_library.string_count_question()
        elif question_num == 5:
            question_library.perfect_numbers_question()
        elif question_num == 6:
            question_library.factorial_sum_question()
        elif question_num == 7:
            question_library.diamond_question()
        elif question_num == 8:
            question_library.sort_numbers_question()
        elif question_num == 9:
            question_library.insert_number_question()
        elif question_num == 10:
            question_library.circle_question()
    except Exception as e:
        msgbox.showerror("错误", f"运行题目 {question_num} 时出现错误：{str(e)}")

root = tk.Tk()
root.title("题库")
root.geometry("600x500")

# 使用ttkthemes库应用主题（确保已安装ttkthemes库，通过pip install ttkthemes安装）
from ttkthemes import ThemedStyle
style = ThemedStyle(root)
style.set_theme("equilux")  # 可替换为其他喜欢的主题，如"plastik"、"arc"等

# 定义不同用途的字体样式
title_font = tkFont.Font(family="微软雅黑", size=20, weight="bold")
button_font = tkFont.Font(family="微软雅黑", size=14)
sub_text_font = tkFont.Font(family="微软雅黑", size=12)

# 创建主框架，用于统一管理界面布局与添加内边距
main_frame = tk.Frame(root, bg="#F8F8F8")
main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)

# 配置主框架的列和行权重，便于组件自适应布局
main_frame.grid_columnconfigure(0, weight=1)
main_frame.grid_rowconfigure(0, weight=1)

# 创建标题标签，设置样式并放置在布局中
title_label = tk.Label(main_frame, text="编程题目选择", font=title_font, fg="#333333", bg="#F8F8F8")
title_label.grid(row=0, column=0, pady=(10, 20), sticky="n")

# 创建一个容器框架，用于放置题目按钮，添加背景色和边框使其更突出
button_container = tk.Frame(main_frame, bg="#FFFFFF", bd=2, relief=tk.SOLID)
button_container.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")

# 配置按钮容器的列权重，让按钮在水平方向自适应拉伸
button_container.grid_columnconfigure(0, weight=1)

# 循环添加题目对应的按钮，应用自定义样式，并设置按钮的布局参数
for i in range(1, 11):
    question_text = question_library.get_question_text(i)
    button = tk.Button(button_container, text=f"题目 {i}: {question_text}",
                       command=lambda num=i: run_question(num),
                       font=button_font, bg="#E6E6E6", fg="#333333", relief=tk.GROOVE,
                       activebackground="#D9D9D9", activeforeground="#333333",
                       width=40, height=2)
    button.grid(row=i, column=0, pady=8, sticky="ew")

# 创建一个提示标签，用于显示操作提示信息，设置样式并放置在布局底部
tip_label = tk.Label(main_frame, text="点击相应题目按钮开始答题，点击退出按钮关闭程序",
                     font=sub_text_font, fg="#666666", bg="#F8F8F8")
tip_label.grid(row=2, column=0, pady=(0, 10), sticky="s")

# 创建退出按钮，设置独特样式使其突出显示，并放置在布局右下角
exit_button = tk.Button(main_frame, text="退出", command=root.quit, font=button_font, bg="#FF5252", fg="white",
                        relief=tk.RAISED, bd=3,
                        activebackground="#E04040", activeforeground="white",
                        width=10, height=1)
exit_button.place(relx=0.95, rely=0.95, anchor=tk.SE)

root.mainloop()